/*     */ package com.newcrm.util;
/*     */ 
/*     */ import com.newcrm.db.ConnectionManager;
/*     */ import com.newcrm.db.UserVOIPManager;
/*     */ import com.newcrm.db.VOIPCallManager;
/*     */ import java.io.IOException;
/*     */ import java.net.URLEncoder;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import net.sf.json.JSONArray;
/*     */ import net.sf.json.JSONObject;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.log4j.Logger;
/*     */ import org.jsoup.Connection;
/*     */ import org.jsoup.Jsoup;
/*     */ 
/*     */ public class UCPAASUtil
/*     */ {
/*  35 */   private static Logger logger = Logger.getLogger(UCPAASUtil.class.getName());
/*     */ 
/*     */   public static int loadCallLog(String appId, String clientId, Date startDate, Date endDate, String tel, int companyId, int page, boolean isSinglePage)
/*     */   {
/*  41 */     java.sql.Connection conn = ConnectionManager.getInstance().getConnection();
/*  42 */     if (conn == null) {
/*  43 */       return 0;
/*     */     }
/*     */ 
/*     */     try
/*     */     {
/*  48 */       Map agents = new HashMap();
/*     */ 
/*  51 */       UserVOIPManager userVOIPManager = new UserVOIPManager();
/*  52 */       List voipNames = userVOIPManager
/*  53 */         .getUsersVOIPName(conn, companyId);
/*  54 */       if ((voipNames != null) && (voipNames.size() > 0)) {
                for (Object voipNameO : voipNames) {
                    Map voipName = (Map)voipNameO;
/*  56 */           agents.put((String)voipName.get("UserName"), 
/*  57 */             (String)voipName.get("UserId"));
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/*  62 */       int count = getCallLog(conn, appId, clientId, tel, startDate, 
/*  63 */         endDate, companyId, page, isSinglePage, agents);
/*     */ 
/*  65 */       if (isSinglePage) {
/*  66 */         return count;
/*     */       }
/*     */ 
/*  70 */       VOIPCallManager manager = new VOIPCallManager();
/*  71 */       manager.addCallLog(conn, startDate, endDate, count, companyId);
/*     */     } finally {
/*     */       try {
/*  74 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  76 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */     try
/*     */     {
/*  74 */       DbUtils.close(conn);
/*     */     } catch (SQLException e) {
/*  76 */       logger.warn("close conn failed", e);
/*     */     }
/*     */ 
/*  80 */     return 0;
/*     */   }
/*     */ 
/*     */   public static int getCallLog(java.sql.Connection conn, String appId, String clientId, String tel, Date startDate, Date endDate, int companyId, int page, boolean isSinglePage, Map<String, String> agents)
/*     */   {
/* 104 */     String date = Util.date2Str(new Date());
/* 105 */     String sign = MD5.getMD5(appId + "_qikebao_" + date);
/*     */ 
/* 107 */     if (clientId == null) {
/* 108 */       clientId = "";
/*     */     }
/* 110 */     if (tel == null) {
/* 111 */       tel = "";
/*     */     }
/*     */     try
/*     */     {
/* 115 */       int offset = (page - 1) * 20;
/*     */ 
/* 117 */       String url = "http://ucpaas.qikebao.com/callrecord?appId=" + 
/* 118 */         URLEncoder.encode(appId, "UTF-8") + "&sign=" + 
/* 119 */         URLEncoder.encode(sign, "UTF-8") + "&date=" + 
/* 120 */         URLEncoder.encode(date, "UTF-8") + "&clientId=" + 
/* 121 */         URLEncoder.encode(clientId, "UTF-8") + "&tel=" + 
/* 122 */         URLEncoder.encode(tel, "UTF-8") + "&offset=" + offset + 
/* 123 */         "&start=" + 
/* 124 */         URLEncoder.encode(Util.date2Str(startDate), "UTF-8") + 
/* 125 */         "&end=" + 
/* 126 */         URLEncoder.encode(Util.date2Str(endDate), "UTF-8");
/*     */ 
/* 128 */       Connection.Response response = Jsoup.connect(url).userAgent("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB6.6; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; .NET CLR 1.1.4322; .NET4.0C)")
/* 129 */         .timeout(10000).method(Connection.Method.GET).execute();
/*     */ 
/* 132 */       JSONObject jsonObject = JSONObject.fromObject(response.body());
/* 133 */       int num = Util.getJSONIntValue(jsonObject, "num");
/* 134 */       int totalNum = Util.getJSONIntValue(jsonObject, "totalNum");
/*     */ 
/* 136 */       JSONArray list = jsonObject.getJSONArray("items");
/*     */ 
/* 138 */       if (num == 0) {
/* 139 */         return totalNum;
/*     */       }
/*     */ 
/* 142 */       List baseParams = new ArrayList();
/* 143 */       for (int i = 0; i < list.size(); i++) {
/* 144 */         JSONObject item = list.getJSONObject(i);
/* 145 */         List info = new ArrayList();
/*     */ 
/* 147 */         info.add(Util.getJSONStringValue(item, "CallId"));
/*     */ 
/* 150 */         String callUserId = (String)agents.get(Util.getJSONStringValue(item, 
/* 151 */           "CallerCode"));
/* 152 */         if (callUserId == null) {
/* 153 */           callUserId = "";
/*     */         }
/* 155 */         info.add(callUserId);
/*     */ 
/* 157 */         info.add(Util.getJSONStringValue(item, "CalledCode"));
/* 158 */         info.add(Integer.valueOf(1));
/* 159 */         String callStartDate = Util.getJSONStringValue(item, 
/* 160 */           "StartDate");
/* 161 */         String callEndDate = Util.getJSONStringValue(item, "EndDate");
/* 162 */         if ((callStartDate == null) || (callStartDate.trim().length() == 0)) {
/* 163 */           callStartDate = callEndDate;
/*     */         }
/*     */ 
/* 166 */         info.add(callStartDate);
/*     */ 
/* 168 */         info.add(callEndDate);
/*     */ 
/* 170 */         info.add(Integer.valueOf(Util.getJSONIntValue(item, "CallTime")));
/* 171 */         String fileUrl = Util.getJSONStringValue(item, "FileUrl");
/*     */ 
/* 173 */         if ((fileUrl != null) && (fileUrl.trim().length() > 0))
/* 174 */           info.add("http://www.ucpaas.com/fileserver/record/" + 
/* 175 */             fileUrl);
/*     */         else {
/* 177 */           info.add("");
/*     */         }
/*     */ 
/* 180 */         info.add("");
/* 181 */         info.add(Integer.valueOf(companyId));
/* 182 */         info.add(callUserId);
/* 183 */         num++;
/* 184 */         baseParams.add(info.toArray(new Object[0]));
/*     */       }
/*     */ 
/* 187 */       VOIPCallManager manager = new VOIPCallManager();
/* 188 */       manager.add(conn, baseParams);
/*     */ 
/* 190 */       if (isSinglePage) {
/* 191 */         return totalNum;
/*     */       }
/*     */ 
/*     */       try
/*     */       {
/* 196 */         Thread.sleep(3000L);
/*     */       }
/*     */       catch (InterruptedException localInterruptedException) {
/*     */       }
/* 200 */       getCallLog(conn, appId, clientId, tel, startDate, endDate, 
/* 201 */         companyId, page + 1, isSinglePage, agents);
/*     */     }
/*     */     catch (IOException e) {
/* 204 */       logger.warn("get voip call info failed", e);
/*     */     }
/*     */ 
/* 207 */     return 0;
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.util.UCPAASUtil
 * JD-Core Version:    0.6.1
 */